package cn.hchaojie.camp.day02.exercise;

/**
 * 素数相关的练习
 * 
 * @author hchaojie
 *
 */
public class Prime {
	public static void main(String[] args) {
		System.out.println("素数测试：");
		System.out.println(isPrime(2));
		System.out.println(isPrime(3));
		System.out.println(isPrime(4));
		System.out.println(isPrime(5));
		System.out.println(isPrime(6));
		
		System.out.println("100以内的素数：");
		for (int i = 2; i <= 100; i++) {
			if (isPrime(i)) {
				System.out.println(i);
			}
		}
		
		System.out.println("打印50个素数：");
		for (int count = 1, i = 2; count <= 50; i++) {
			if (isPrime(i)) {
				System.out.println("第" + count + "个：" + i);
				count++;
			}
		}
	}
	
	public static boolean isPrime(int a) {
		if (a <= 1) {		// 1 不是素数
			return false;
		}
		
		for (int i = 2; i < a; i++) {
			if (a % i == 0) {
				return false;
			}
		}
		
		return true;
	}
}
